//
//  GameContorllerDelegate.m
//  AnotherFire
//
//  Created by pcwiz on 23/06/09.
//  Copyright 2009 leowiz. All rights reserved.
//

#import "GameContorllerDelegate.h"
#import "GameLogic.h"

@implementation GameContorllerDelegate
@synthesize gl;

-(void) dealloc
{
	[self.gl release];
	[super dealloc];
}

-(id) initWithNames: (NSString *)_name1 second:(NSString *)_name2
{
	self.gl = [[GameLogic alloc] initWithGroups: 10 width: 8 group1: _name1 group2: _name2];
	return self;
}

- (NSMutableArray *) getBoard
{
	return self.gl.board;
}


- (void) touchOccured: (CGPoint) _where;
{
	printf("A touch occured at: %f, %f.\n", _where.x, _where.y);
	int col_width = self.view.frame.size.width / [self.gl width];
	int where = floor(_where.x / col_width);
		
	int height = [self.gl moveTo:where];
	
	if (-1 != height)
	{
		[self.view setNeedsDisplay];
	}
	else
	{
		UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Bad move!" message:@"You can't put a coin in a full row." delegate:self cancelButtonTitle:@"Understood!" otherButtonTitles:nil];
		[av show];
		[av release];
	}
	
	NSString *str = [gl whoWon];
	if (@"" != str)
	{
		NSString *msg = [str  stringByAppendingString: @" won. Laugh at the other player."];
		UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"We have a winner!" message:msg delegate:self cancelButtonTitle:@"New Game" otherButtonTitles:nil];
		[av setDelegate:self];
		[av show];
		[av release];
	}
	
}
@end
